package com.wclass.controller.app;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.wclass.common.ReturnObject;
import com.wclass.entity.BaseCollect;
import com.wclass.entity.BaseFindFilename;
import com.wclass.entity.CourseInfo;
import com.wclass.entity.PostsInfo;
import com.wclass.interceptor.BaseContextHandler;
import com.wclass.service.IBaseCollectService;
import com.wclass.service.IBaseFindFilenameService;
import com.wclass.service.ICourseInfoService;
import com.wclass.service.IPostsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author SEN
 * @since 2018-12-26
 */
@Api(description = "收藏管理-【课件+发现】")
@RestController
@RequestMapping("/baseCollect")
public class BaseCollectController {
    @Autowired
    private IBaseCollectService iBaseCollectService;
    @Autowired
    private IBaseFindFilenameService iBaseFindFilenameService;
    @Autowired
    private IPostsInfoService iPostsInfoService;
    @Autowired
    private ICourseInfoService iCourseInfoService;

    @ApiOperation("获取我的收藏列表分页（1发现2课程）")
    @RequestMapping(value = "/getMyCollectPage", method = RequestMethod.POST)
    public ReturnObject<Object> getMyCollectPage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") Integer limit,
                                                 @ApiParam("页数") @RequestParam(defaultValue = "1") Integer offset,
                                                 @ApiParam("类型 1发现2课程【必传】") @RequestParam Integer type,
                                                 @ApiParam("文件夹id") Long findFileId) {
        List<PostsInfo> postsInfos = Lists.newArrayList();//发现
        List<CourseInfo> courseInfos = Lists.newArrayList();//课程

        EntityWrapper<BaseCollect> ew = new EntityWrapper<>();
        ew.eq("type", type);
        if (findFileId != null) {
            ew.eq("find_file_id", findFileId);
        }
        ew.eq("user_id", BaseContextHandler.getUserID());
        Page<BaseCollect> obj = iBaseCollectService.selectPage(new Page<>(offset, limit), ew);

        for (BaseCollect info : obj.getRecords()) {
            if (type == 1) {
                PostsInfo postsInfo = iPostsInfoService.selectById(info.getMoldId());
                if (postsInfo != null)
                    postsInfos.add(postsInfo);
            }
            if (type == 2) {
                CourseInfo courseInfo = iCourseInfoService.selectById(info.getMoldId());
                if (courseInfo != null)
                    courseInfos.add(courseInfo);
            }
        }

        if (type == 1) {
            return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), postsInfos);
        }
        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), courseInfos);
    }

    @ApiOperation("收藏或者取消收藏")
    @RequestMapping(value = "/addOrDel", method = RequestMethod.POST)
    public ReturnObject addCollect(@ApiParam("收藏id（发现id 或者 课程id）【必传】") @RequestParam Long moldId,
                                   @ApiParam("名称（发现主题 或者 课程名称）【必传】") @RequestParam String moldName,
                                   @ApiParam("类型 1 发现 2 课程【必传】") @RequestParam Integer type,
                                   @ApiParam("文件夹id") Long findFileId) {
        List<BaseCollect> list = iBaseCollectService.selectList(new EntityWrapper<BaseCollect>().eq("user_id",
                BaseContextHandler.getUserID()).eq("type", type).eq("mold_id", moldId));
        if (list.size() > 0) {
            BaseCollect info = list.get(0);
            findFileId = info.getFindFileId();
            //取消收藏
            iBaseCollectService.deleteById(info.getId());

            //封面修改
            List<BaseCollect> collectListist = iBaseCollectService.selectList(new EntityWrapper<BaseCollect>().eq("user_id",
                    BaseContextHandler.getUserID()).eq("find_file_id", findFileId).orderBy("crt_time", false));
            if (collectListist.size() > 0) {
                BaseCollect collect = collectListist.get(0);
                BaseFindFilename findFilename = iBaseFindFilenameService.selectById(findFileId);
                String coverImg;
                if (collect.getType() == 1) {
                    PostsInfo postsInfo = iPostsInfoService.selectById(collect.getMoldId());
                    coverImg = postsInfo.getCover();
                } else {
                    CourseInfo courseInfo = iCourseInfoService.selectById(collect.getMoldId());
                    coverImg = courseInfo.getCoverImgUrl();
                }
                findFilename.setCoverImg(coverImg);
                iBaseFindFilenameService.updateById(findFilename);
            }

            return new ReturnObject<Object>(0, "取消成功", "");
        } else {
            BaseCollect info = new BaseCollect();
            info.setUserId(BaseContextHandler.getUserID());
            info.setMoldId(moldId);
            info.setMoldName(moldName);
            info.setType(type);
            info.setFindFileId(findFileId);
            iBaseCollectService.insert(info);

            BaseFindFilename findFilename = iBaseFindFilenameService.selectById(findFileId);
            if (findFilename != null) {
                String coverImg;
                if (type == 1) {
                    PostsInfo postsInfo = iPostsInfoService.selectById(moldId);
                    coverImg = postsInfo.getCover();
                } else {
                    CourseInfo courseInfo = iCourseInfoService.selectById(moldId);
                    coverImg = courseInfo.getCoverImgUrl();
                }
                findFilename.setCoverImg(coverImg);
                iBaseFindFilenameService.updateById(findFilename);
            }
        }
        return new ReturnObject(0, "添加成功");
    }


    @ApiOperation("创建收藏文件夹-发现")
    @RequestMapping(value = "/addFindFileName", method = RequestMethod.POST)
    public ReturnObject addFindFileName(@ApiParam("文件夹名【必传】") @RequestParam String findFileName) {

        BaseFindFilename info = new BaseFindFilename();
        info.setName(findFileName);
        info.setUserId(BaseContextHandler.getUserID());
        iBaseFindFilenameService.insert(info);
        return new ReturnObject(0, "添加成功");
    }

    @ApiOperation("编辑收藏文件夹-发现")
    @RequestMapping(value = "/editFindFileName", method = RequestMethod.POST)
    public ReturnObject editFindFileName(@ApiParam("id") @RequestParam Long id,
                                         @ApiParam("文件夹名【必传】") @RequestParam String findFileName) {

        BaseFindFilename info = new BaseFindFilename();
        info.setId(id);
        info.setName(findFileName);
        iBaseFindFilenameService.updateById(info);
        return new ReturnObject(0, "编辑成功");
    }

    @ApiOperation("删除收藏文件夹-发现")
    @RequestMapping(value = "/delFindFileName", method = RequestMethod.POST)
    public ReturnObject<Object> delFindFileName(@ApiParam("文件夹id【必传】") @RequestParam Long findFileId) {
        iBaseFindFilenameService.deleteById(findFileId);
        return new ReturnObject<Object>(0, "删除成功", "");
    }

    @ApiOperation("获取发现收藏文件夹列表分页")
    @RequestMapping(value = "/getMyFindFileNamePage", method = RequestMethod.POST)
    public ReturnObject<List<BaseFindFilename>> getMyFindFileNamePage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") Integer limit,
                                                                      @ApiParam("页数") @RequestParam(defaultValue = "1") Integer offset) {
        EntityWrapper<BaseFindFilename> ew = new EntityWrapper<>();
        ew.eq("user_id", BaseContextHandler.getUserID());
        Page<BaseFindFilename> obj = iBaseFindFilenameService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }


}

